'''一元线性回归：拟合广告费与销售额数据'''

import numpy as np
from matplotlib import pyplot as plt
from sklearn.linear_model import LinearRegression

plt.rcParams['font.sans-serif']=['SimHei']  #用来正常显示中文
plt.rcParams['axes.unicode_minus']=False    #用来正常显示负号

x = np.array([10, 13, 22, 37, 45, 48, 59, 65, 66, 68, 68, 71, 84, 88, 89, 89])
y = np.array([19, 60, 71, 74, 69, 89, 146, 130, 153, 144, 128, 123, 127, 125, 154, 150])
'''x广告费，y销售额'''
# print(x.shape)
# print(y.shape)

'''线性回归'''
lin_reg = LinearRegression()
x_train = x.reshape(-1, 1)
y_truan = y.reshape(-1, 1)
lin_reg.fit(x_train, y_truan)
a = lin_reg.coef_[0]         #系数矩阵
b = lin_reg.intercept_       #截距

'''可视化'''
plt.scatter(x, y)
plt.axis([0, 90, 0, 160])
plt.plot(x, a * x + b, color = 'r')
plt.xlabel("广告费用/万元")
plt.ylabel("销售费用/万元")
plt.title("某广告公司广告费用-销售额关系")
plt.show()